//
//  CustomerFaceGatherViewController.m
//  Rail
//
//  Created by hychen on 2018/7/15.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "CustomerFaceGatherViewController.h"

#import "TextFieldTableViewCell.h"
#import "ButtonTableViewCell.h"
#import "SelectPhotoTableViewCell.h"

@interface CustomerFaceGatherViewController ()
@property (nonatomic, copy) NSString *customName;
@property (nonatomic, copy) NSString *customIdCard;
@property (nonatomic, strong) NSMutableArray *selectPhotoArray;
@property (nonatomic, assign) BOOL isUploading;
@end

@implementation CustomerFaceGatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"访客采集"];
    [self resignGesture];
    [self.tableView reloadData];    
}

- (void)resignGesture {
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    gestureRecognizer.numberOfTapsRequired = 1;
    gestureRecognizer.cancelsTouchesInView = NO;
    [self.tableView addGestureRecognizer:gestureRecognizer];
}

- (void)hideKeyboard {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BMWeakSelf
    if (indexPath.row == 0) {
        TextFieldTableViewCell *cell = [TextFieldTableViewCell cellWithInitStyleTableView:tableView class:[TextFieldTableViewCell class]];
        cell.title = @"访客姓名:";
//        cell.content = [self.customName objectIsNotEmpty]?self.customName:@"请输入访客姓名";
        cell.content = self.customName;
        cell.placeholder = @"请输入访客姓名";
        cell.tf.keyboardType = UIKeyboardTypeDefault;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setBlockEndEditing:^(NSString *text) {
            weakSelf.customName = text;
            [weakSelf.tableView reloadData];
        }];
        return cell;
    }
    if (indexPath.row == 1) {
        TextFieldTableViewCell *cell = [TextFieldTableViewCell cellWithInitStyleTableView:tableView class:[TextFieldTableViewCell class]];
        cell.title = @"身份证号码:";
        cell.content = self.customIdCard;
        cell.placeholder = @"请输入身份证号码";
        cell.tf.keyboardType = UIKeyboardTypeNumberPad;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        __weak __typeof(cell) weakCell = cell;
        [cell setBlockEndEditing:^(NSString *text) {
            if (text.length != 18) {
                Show_Msg_Error(@"请输入正确18位身份证号码")
                weakCell.tf.text = text;
                return;
            }
            weakSelf.customIdCard = text;
            [weakSelf.tableView reloadData];
        }];
        return cell;
    }
    if (indexPath.row == 2) {
        SelectPhotoTableViewCell *cell = [SelectPhotoTableViewCell cellWithInitStyleTableView:tableView class:[SelectPhotoTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.maxCount = 3;
        [cell setTitle:@"人脸照片"];
        [cell setPhotoArray:self.selectPhotoArray];
        BMWeakSelf
        [cell setBlockSelectPhotoArray:^(NSArray *photoArray) {
            weakSelf.selectPhotoArray = [NSMutableArray arrayWithArray:photoArray];
        }];
        return cell;
    }
    if (indexPath.row == 3) {
        ButtonTableViewCell *cell = [ButtonTableViewCell cellWithTableView:tableView class:[ButtonTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.buttonTitle = @"保存";
        BMWeakSelf
        [cell setBlockClickEventButton:^{
            [weakSelf requestFaceGather];
        }];
        
        return cell;
    }
    return [UITableViewCell new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 50.f;
    }
    else if (indexPath.row == 1) {
        return 50.f;
    }
    else if (indexPath.row == 2) {
        return 160.f;
    }
    else if (indexPath.row == 3) {
        return 200.f;
    }
    else {
        return 0;
    }
}

- (void)requestFaceGather {
    if (self.isUploading) {
        return;
    }
    
    if (![self.customName objectIsNotEmpty]) {
        Show_Msg_Error(@"请输入访客姓名")
        return;
    }
    
    if (![self.customIdCard objectIsNotEmpty]) {
        if (![CheckUtility checkForIdCard:self.customIdCard] || self.customIdCard.length != 18) {
            Show_Msg_Error(@"身份证号码格式不正确")
            return;
        }
        
        Show_Msg_Error(@"请输入身份证号码")
        return;
    }
    
    if (self.selectPhotoArray.count < 1) {
        Show_Msg_Error(@"请选择图片")
        return;
    }
    
    BMWeakSelf
    if (![BMUtils checkOpen4gAutoUploadWithCompletion:^{
        [weakSelf _requestFaceGather];
    }]) {
        return;
    }
    
    [self _requestFaceGather];
}

- (void)_requestFaceGather {
    BMWeakSelf
    
    UIImage *image1 = nil;
    if (self.selectPhotoArray.count > 0) {
        SelectPhotoModel *imageModel = [self.selectPhotoArray objectAtIndex:0];
        image1 = [imageModel uploadImage]?:[imageModel image];
    }
    
    UIImage *image2 = nil;
    if (self.selectPhotoArray.count > 1) {
        SelectPhotoModel *imageModel = [self.selectPhotoArray objectAtIndex:1];
        image2 = [imageModel uploadImage]?:[imageModel image];
    }
    
    UIImage *image3 = nil;
    if (self.selectPhotoArray.count > 2) {
        SelectPhotoModel *imageModel = [self.selectPhotoArray objectAtIndex:2];
        image3 = [imageModel uploadImage]?:[imageModel image];
    }
    
    self.isUploading = YES;
    
    Show_Msg(@"采集中")
    
    [HttpRequest uploadGatherFaceUserDataWithUserId:self.customIdCard type:1 visitorName:self.customName image1:image1 image2:image2 image3:image3 success:^(id object) {
        weakSelf.isUploading = NO;
        Show_Msg_Success(@"采集成功")
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (weakSelf.BlockGatherCustomerSuccess) {
                weakSelf.BlockGatherCustomerSuccess();
            }
            
            Dismiss_NoMsg
            
            if (weakSelf.isFromRegather && weakSelf.navigationController.viewControllers.count > 2) {
                NSMutableArray *array = [NSMutableArray arrayWithArray:weakSelf.navigationController.viewControllers];
                [array removeObjectAtIndex:weakSelf.navigationController.viewControllers.count-2];
                weakSelf.navigationController.viewControllers = array.copy;
            }
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        });
    } failure:^(NSString *code, NSString *msg) {
        weakSelf.isUploading = NO;
        Show_Msg_Error(msg)
    }];
    
}

- (NSMutableArray *)selectPhotoArray {
    if (!_selectPhotoArray) {
        _selectPhotoArray = [NSMutableArray array];
    }
    return _selectPhotoArray;
}

- (NSString *)customName {
    if (!_customName) {
        _customName = @"";
    }
    return _customName;
}

- (NSString *)customIdCard {
    if (!_customIdCard) {
        _customIdCard = @"";
    }
    return _customIdCard;
}

- (void)setUserModel:(SelectUserInfoModel *)userModel {
    _userModel = userModel;
    self.customName = userModel.visitor_name;
    self.customIdCard = userModel.user_account;
    [self.tableView reloadData];
}
@end
